import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    startTime: dayjs().format('HH:mm:ss'),
    endTime: dayjs().format('HH:mm:ss'),
    delBtnShow: true,
    isTimePas: false,
    // 会议/休息/就餐 (1/2/3)
    array: ['会议', '休息', "就餐"],
    listIndex: null,
    list: []
  },
  onLoad() {
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    if (prevPage.data.form.nonProdTimeMgtDTOList) {
      let list = prevPage.data.form.nonProdTimeMgtDTOList
      for (let i = list.length; i > 0; i--) {
        list[i - 1].title = '非生产时间' + i
        list[i - 1].beginEndTime = list[i - 1].beginTime + "至" + list[i - 1].endTime
      }
      this.setData({
        list: prevPage.data.form.nonProdTimeMgtDTOList
      })
    }
  },

  addList() {
    let obj = {
      beginEndTime: '',
      type: null,
      beginTime: '',
      endTime: '',
      title: '非生产时间' + (this.data.list.length + 1)
    }
    this.data.list.unshift(obj)
    this.setData({
      list: this.data.list
    })
  },

  delList(e) {
    console.log(e.target.dataset.index)
    let ind = e.target.dataset.index
    this.data.list.splice(ind, 1)
    this.setData({
      list: this.data.list
    })
  },

  showSelectTime(e) {
    console.log(e)
    this.data.listIndex = e.target.dataset.index
    this.setData({
      isTimePas: true
    })
  },

  onAffirm(data) {
    console.log(data)
    let ind = this.data.listIndex
    if (data.result) {
      this.data.list[ind].beginTime = data.startTime
      this.data.list[ind].endTime = data.endTime
      this.data.list[ind].beginEndTime = data.startTime + "至" + data.endTime
      this.setData({
        list: this.data.list
      });
      const pages = getCurrentPages();
      let currPage = pages[pages.length - 1]; //当前页面
      let prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.data.form.nonProdTimeMgtDTOList = this.data.list
    }
    this.setData({
      isTimePas: false
    })
  },

  bindPickerChange(e) {
    console.log(e.target.dataset.index)
    let ind = e.target.dataset.index
    this.data.list[ind].type = e.detail.value + 1
    console.log(this.data.list)
    this.setData({
      list: this.data.list
    });
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.data.form.nonProdTimeMgtDTOList = this.data.list
  }
});
